package com.ruoyi.comparison.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.comparison.domain.TaskJudge;
import com.ruoyi.comparison.domain.TaskRelationList;
import com.ruoyi.comparison.domain.bo.TaskJudgeBo;
import com.ruoyi.comparison.domain.vo.TaskJudgeVo;
import com.ruoyi.comparison.mapper.TaskJudgeMapper;
import com.ruoyi.comparison.service.ITaskJudgeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 裁判文书信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-17
 */
@RequiredArgsConstructor
@Service
public class TaskJudgeServiceImpl implements ITaskJudgeService {

    private final TaskJudgeMapper baseMapper;

    /**
     * 查询裁判文书信息
     */
    @Override
    public TaskJudgeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询裁判文书信息列表
     */
    @Override
    public TableDataInfo<TaskJudgeVo> queryPageList(TaskJudgeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskJudge> lqw = buildQueryWrapper(bo);
        String username = LoginHelper.getUsername();
        lqw.eq(TaskJudge::getCreateBy,username);
        Page<TaskJudgeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询裁判文书信息列表
     */
    @Override
    public List<TaskJudgeVo> queryList(TaskJudgeBo bo) {
        LambdaQueryWrapper<TaskJudge> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TaskJudge> buildQueryWrapper(TaskJudgeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaskJudge> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCaseName()), TaskJudge::getCaseName, bo.getCaseName());
        lqw.eq(StringUtils.isNotBlank(bo.getCaseReason()), TaskJudge::getCaseReason, bo.getCaseReason());
        lqw.eq(StringUtils.isNotBlank(bo.getCaseNo()), TaskJudge::getCaseNo, bo.getCaseNo());
        lqw.eq(StringUtils.isNotBlank(bo.getCaseType()), TaskJudge::getCaseType, bo.getCaseType());
        lqw.eq(StringUtils.isNotBlank(bo.getAmount()), TaskJudge::getAmount, bo.getAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getIsProsecutor()), TaskJudge::getIsProsecutor, bo.getIsProsecutor());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDefendant()), TaskJudge::getIsDefendant, bo.getIsDefendant());
        lqw.eq(StringUtils.isNotBlank(bo.getJudgeResult()), TaskJudge::getJudgeResult, bo.getJudgeResult());
        lqw.eq(StringUtils.isNotBlank(bo.getJudgeDate()), TaskJudge::getJudgeDate, bo.getJudgeDate());
        lqw.eq(StringUtils.isNotBlank(bo.getPublishDate()), TaskJudge::getPublishDate, bo.getPublishDate());
        return lqw;
    }

    /**
     * 新增裁判文书信息
     */
    @Override
    public Boolean insertByBo(TaskJudgeBo bo) {
        TaskJudge add = BeanUtil.toBean(bo, TaskJudge.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改裁判文书信息
     */
    @Override
    public Boolean updateByBo(TaskJudgeBo bo) {
        TaskJudge update = BeanUtil.toBean(bo, TaskJudge.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaskJudge entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除裁判文书信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void insert(List<TaskJudge> result) {
        boolean insert = this.baseMapper.insertBatch(result);
    }

    @Override
    public TableDataInfo<TaskJudgeVo> searchPageList(TaskJudgeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskJudge> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TaskJudge::getSearchKey,bo.getCaseName());
        String username = LoginHelper.getUsername();
        wrapper.eq(TaskJudge::getCreateBy,username);
        Page<TaskJudgeVo> result = baseMapper.selectVoPage(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public void insertOne(TaskJudge taskJudge) {
        this.baseMapper.insert(taskJudge);
    }
}
